<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
/**
 * 请求成功，公共返回方法
 * @param $data
 * @param string $msg
 * @param int $code
 * @param int $isDie
 */
function returnSuccess($data=null,$msg = '成功！',$code = 1,$isDie = 1){
    echo urldecode(json_encode(['code'=>$code,'msg'=>$msg,'data'=>$data]));
    die;
}

/**
 * 请求失败公共返回方法
 * @param $msg
 * @param string $data
 * @param int $code
 */
function returnError($msg,$data='',$code=0){
    echo urldecode(json_encode(['code'=>$code,'msg'=>$msg,'data'=>$data]));
    die;
}

/**
 * 获取真实ip
 */
function get_real_ip()
{
    $ip=false;
    if(!empty($_SERVER["HTTP_CLIENT_IP"]))
    {
        $ip = $_SERVER["HTTP_CLIENT_IP"];
    }
    if (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
    {
        $ips = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
        if ($ip)
        {
            array_unshift($ips, $ip); $ip = FALSE;
        }
        for ($i = 0; $i < count($ips); $i++)
        {
            if (!eregi ("^(10|172/.16|192/.168)/.", $ips[$i]))
            {
                $ip = $ips[$i];
                break;
            }
        }
    }
    return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);
}

/**
 * 根据ip获取所在城市及运营商
 */
function get_login_area($ip = '')
{
    header("Content-Type:text/html;charset=gb2312");
//    $ip = '39.106.69.213';
    if(!empty($ip)) {
        $url = 'http://whois.pconline.com.cn/ipJson.jsp';
        $data['ip'] = $ip;
        $data['json'] = 'true';
        $result = mycurl($url, $data);
        $result = trim($result);
        $result = mb_convert_encoding($result,"UTF-8","GBK");
        $result = json_decode($result, true);
        $addr = $result['addr'];
        return $addr;
    }
}

/**
 * 获取浏览器信息
*/

function browse_info()
{
    if(empty($_SERVER['HTTP_USER_AGENT'])){
        return 'robot！';
    }
    if( (false == strpos($_SERVER['HTTP_USER_AGENT'],'MSIE')) && (strpos($_SERVER['HTTP_USER_AGENT'], 'Trident')!==FALSE) ){
        return 'Internet Explorer 11.0';
    }
    if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'MSIE 10.0')){
        return 'Internet Explorer 10.0';
    }
    if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'MSIE 9.0')){
        return 'Internet Explorer 9.0';
    }
    if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'MSIE 8.0')){
        return 'Internet Explorer 8.0';
    }
    if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'MSIE 7.0')){
        return 'Internet Explorer 7.0';
    }
    if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'MSIE 6.0')){
        return 'Internet Explorer 6.0';
    }
    if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'Edge')){
        return 'Edge';
    }
    if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'Firefox')){
        return 'Firefox';
    }
    if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'Chrome')){
        return 'Chrome';
    }
    if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'Safari')){
        return 'Safari';
    }
    if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'Opera')){
        return 'Opera';
    }
    if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'360SE')){
        return '360SE';
    }
    //微信浏览器
    if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'MicroMessage')){
        return 'MicroMessage';
    }
}

function mycurl($url,$date=null)
{
    $ch=curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    if(!empty($date)){
        curl_setopt($ch, CURLOPT_POST,"1");
        curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($date));
    }
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
    //curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true); // 从证书中检查SSL加密算法是否存在
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); //默认是 0 直接返回获取的输出的文本流
    $result = curl_exec($ch);
    //var_dump($result);
    curl_close($ch);
    return $result;
}

/**
 * 对emoji表情转义
 * @param $str
 * @return string
 */
function emoji_encode($str)
{
    $strEncode = '';
    $length = mb_strlen($str, 'utf-8');
    for ($i = 0; $i < $length; $i++) {
        $_tmpStr = mb_substr($str, $i, 1, 'utf-8');
        if (strlen($_tmpStr) >= 4) {
            $strEncode .= '[[EMOJI:' . rawurlencode($_tmpStr) . ']]';
        } else {
            $strEncode .= $_tmpStr;
        }
    }
    return $strEncode;
}

/**
 * 对emoji表情反转义
 * @param $str
 * @return string|string[]|null
 */
function emoji_decode($str)
{
    $strDecode = preg_replace_callback('|\[\[EMOJI:(.*?)\]\]|', function ($matches) {
        return rawurldecode($matches[1]);
    }, $str);
    return $strDecode;
}
